import java.awt.Cursor;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JTextArea;


public class Console extends JTextArea{

	/**
	 * Default Serialization
	 */
	private static final long serialVersionUID = 1L;

	public Console(){
		super();
		setEditable(false);
	}
	
	public void printText(String text){
		PrototypeGui.frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
		String lines[] = text.split("\n");
		for (String line: lines){
			String toPrint;
			if (getText().equals("")){
				toPrint = line;
			}
			else{
				toPrint = getText() + "\n" + line;
			}
			setText(toPrint);
			long start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 10){}
		}
		PrototypeGui.frame.setCursor(Cursor.getDefaultCursor());
	}
	
	public void saveText(){
		JFileChooser fc = new JFileChooser();
		int val = fc.showOpenDialog(PrototypeGui.frame);
		if (val == JFileChooser.APPROVE_OPTION){
			try{
				BufferedWriter writer = new BufferedWriter(new FileWriter(fc.getSelectedFile()));
				writer.write(getText());
				writer.close();
			} 
			catch (IOException e){
				e.printStackTrace();
			}
		}
	}
}
